package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartMapper shoppingCartMapper;
    @Resource
    private DishMapper dishMapper;
    @Resource
    private SetmealMapper setmealMapper;
    //默认添加数量
    private static final Integer SHOP_NUMBER = 1;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //1.判断购物车表内是否有要添加的商品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //如果已经存在
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart shoppingCartOne = shoppingCartMapper.getOneData(shoppingCart);
        if (!Objects.isNull(shoppingCartOne)){
            shoppingCartOne.setNumber(shoppingCartOne.getNumber() + 1);
            shoppingCartMapper.updata(shoppingCartOne);
        }else{//如果不存在
            if(shoppingCartDTO.getDishId() != null){
                DishVO dishVO = dishMapper.selectDishById(shoppingCartDTO.getDishId());
                //菜品名称
                shoppingCart.setName(dishVO.getName());
                //菜品图片
                shoppingCart.setImage(dishVO.getImage());
                //菜品金额
                shoppingCart.setAmount(dishVO.getPrice());
            }else if(shoppingCartDTO.getSetmealId() != null){
                SetmealVO setmealVO = setmealMapper.selectSetmealById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setAmount(setmealVO.getPrice());
            }else{
                throw new BaseException("传递的参数有误");
            }
            //添加默认数量
            shoppingCart.setNumber(SHOP_NUMBER);
            //添加时间
            shoppingCart.setCreateTime(LocalDateTime.now());

            shoppingCartMapper.add(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> selectShoppingCart() {
        Long id = BaseContext.getCurrentId();
        return shoppingCartMapper.selectShoppingCart(id);
    }

    /**
     * 清空购物车
     */
    @Override
    public void emptyShoppingCart() {

        shoppingCartMapper.emptyShoppingCart(BaseContext.getCurrentId());
    }

    /**
     * 单个删除
     * @param shoppingCartDTO
     */
    @Override
    public void updateShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //获取购物车内菜品(套餐)对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        if(shoppingCart.getDishId() != null){
             shoppingCart = shoppingCartMapper.selectShoppingCartByDishId(shoppingCartDTO.getDishId(),BaseContext.getCurrentId());
        }
        if(shoppingCart.getSetmealId() != null){
             shoppingCart = shoppingCartMapper.selectShoppingCartBySetmealId(shoppingCartDTO.getSetmealId(),BaseContext.getCurrentId());
        }

        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        if(shoppingCart.getNumber() <= 0){
            shoppingCartMapper.updateShoppingCart(shoppingCart.getId());
        }else {
            shoppingCartMapper.updata(shoppingCart);
        }

    }
}
